package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.RoleType;
import com.cloudfun.campusshare.common.model.dto.BatchOperateDTO;
import com.cloudfun.campusshare.common.model.dto.FeedbackDTO;
import com.cloudfun.campusshare.common.model.dto.FeedbackSearchDTO;
import com.cloudfun.campusshare.common.model.vo.FeedbackExportVO;
import com.cloudfun.campusshare.common.model.vo.FeedbackListVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.FeedbackService;
import com.cloudfun.campusshare.util.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by CodeGenerator on 2020/05/27
 */
@Slf4j
@RestController
@AuthRequired
@RequestMapping("/v1/Feedback")
@Api("意见反馈模块")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @PostMapping
    @ApiOperation(value = "创建")
    @AuthRequired(role = {RoleType.OPERATIONAL_MERCHANT, RoleType.CUSTOMER})
    public BaseResult<?> create(@ApiParam(value = "FeedbackDTO", required = true) @Valid @RequestBody FeedbackDTO feedbackDTO) {
        feedbackService.create(feedbackDTO);
        return BaseResultFactory.produceSuccess();
    }

    @GetMapping("/list")
    @ApiOperation(value = "分页列表")
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<PageResult<FeedbackListVO>> list(Pageable pageable, @ApiParam(value = "searchDTO", required = true) FeedbackSearchDTO searchDTO) {
        PageResult<FeedbackListVO> pageResult = feedbackService.search(pageable, searchDTO);
        return BaseResultFactory.produceResult(Code.SUCCESS, pageResult);
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<?> list(@RequestParam String id) {
        feedbackService.delete(id);
        return BaseResultFactory.produceSuccess();
    }

    @PutMapping("/export")
    @ApiOperation("导出")
    @AuthRequired(role = RoleType.ADMIN)
    BaseResult<?> export(HttpServletResponse response, @RequestBody BatchOperateDTO<FeedbackSearchDTO> batchOperateDTO) throws Exception {
        if (CollectionUtils.isEmpty(batchOperateDTO.getIds()) && null == batchOperateDTO.getSearchData() && !BooleanUtils.isTrue(batchOperateDTO.getAll())) {
            return BaseResultFactory.produceEmptyResult(Code.PARAMETER_ERROR, "参数错误");
        }
        long start = System.currentTimeMillis();
        List<FeedbackExportVO> exportVOList;
        if (!CollectionUtils.isEmpty(batchOperateDTO.getIds())) {
            exportVOList = feedbackService.export(batchOperateDTO.getIds(), null);
        } else if (null != batchOperateDTO.getSearchData()) {
            exportVOList = feedbackService.export(null, batchOperateDTO.getSearchData());
        } else {
            exportVOList = feedbackService.export(null, null);
        }
        log.debug("导出{}个意见反馈信息记录耗时：{}ms", exportVOList.size(), (System.currentTimeMillis() - start));
        ExcelUtils.exportExcel(exportVOList, null, "意见反馈", FeedbackExportVO.class, "意见反馈", response);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

}